package coml.zhouxiaoge.zookeeper;

/**
 * @author zhouxiaoge
 * <p>
 * Created by 2022-04-27 22:00
 */
public class DistributedLockTest {
    public static void main(String[] args) throws Exception {
        DistributedLock lock1 = new DistributedLock();

        DistributedLock lock2 = new DistributedLock();

        DistributedLock lock3 = new DistributedLock();

        System.out.println("--------------------");
        new Thread(() -> {
            try {
                lock1.zkLock();
                System.out.println("线程1 启动，获取到锁");
                Thread.sleep(5 * 1000);
                lock1.unZkLock();
                System.out.println("线程1 释放锁");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).start();

        new Thread(() -> {
            try {
                lock2.zkLock();
                System.out.println("线程2 启动，获取到锁");
                Thread.sleep(5 * 1000);
                lock2.unZkLock();
                System.out.println("线程2 释放锁");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).start();

        new Thread(() -> {
            try {
                lock3.zkLock();
                System.out.println("线程3 启动，获取到锁");
                Thread.sleep(5 * 1000);
                lock3.unZkLock();
                System.out.println("线程3 释放锁");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).start();
    }
}
